package com.thwcompany;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Future123 {
    public static void main(String[] args) {
        // 创建自定义线程池
        ExecutorService executor = Executors.newFixedThreadPool(5);

        // 创建一个 CompletableFuture 实例，使用自定义线程池执行任务
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            System.out.println("Task is running in background");
        }, executor);

        // 其他代码...


        // 关闭线程池
        executor.shutdown();

        // 可选：等待任务完成
        try {
            Object o=future.get();
            System.out.println(o);//null
            System.out.println("Task has completed");
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
}
